# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 13:42:34 2022

@author: obiri
"""

import mpctools as mpc
import casadi
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import time
import sys

font = {'family' : 'monospace', 'weight' : 'bold', 'size'   : 18}
mpl.rc('font', **font)  # pass in the font dict as kwargs
mpl.rcParams['axes.linewidth'] = 3.0

Delta = 60 # Time step
Nsim = 50
tplot = np.arange(Nsim+1)*Delta 

###############STATE ERROR EVOLUTION###############
error_case1 = np.load('sum_error1.npy' )
error_case2 = np.load('sum_error2.npy' )
error_case3 = np.load('sum_error3.npy' )


fig_3, axs = plt.subplots(1, figsize=(10,10))
axs.plot(tplot[:-1], error_case1,'-.b' , linewidth=3.5)
axs.plot(tplot[:-1], error_case2,':k' , linewidth=3.5)
axs.plot(tplot[:-1], error_case3,'--g' , linewidth=3.5)
axs.set_ylabel('Error', weight='bold', family='monospace', size=15, labelpad=9)
axs.set_xlim([0, 1000])
axs.grid(linewidth=0.5, linestyle='-.')

fig_3.legend(['Case1', 'Case2', 'Case3'], ncol=3, loc='upper center',frameon=False)
plt.xlabel('Time(min)', weight='bold', family='monospace', size=15, labelpad=9)
fig_3.tight_layout()
plt.show()
# plt.savefig('test.pdf')




# ###############PARAMETER ERROR EVOLUTION###############
P_error_case1 = np.load('sum_Perror1.npy' )
P_error_case2 = np.load('sum_Perror2.npy' )
P_error_case3 = np.load('sum_Perror3.npy' )

fig_1, axs = plt.subplots(1, figsize=(10,10))
axs.plot(tplot[:-1], P_error_case1,'-.b' , linewidth=3.5)
axs.plot(tplot[:-1], P_error_case2,':k' , linewidth=3.5)
axs.plot(tplot[:-1], P_error_case3,'--g' , linewidth=3.5)
axs.grid(linewidth=0.5, linestyle='-.')
axs.set_ylabel('Error', weight='bold', family='monospace', size=15, labelpad=9)
axs.set_xlim([0, 2000])
axs.grid(linewidth=0.5, linestyle='-.')


fig_1.legend(['Case1','Case2', 'Case3'], ncol=3, loc='upper center',frameon=False)
plt.xlabel('Time(min)', weight='bold', family='monospace', size=15, labelpad=9)
fig_1.tight_layout()
plt.show()


#Case1 average RMSE for different  x0
arr1 = np.array([0.02773, 0.02777, 0.02858, 0.02850, 0.02875])
Sum1 = np.sum(arr1)
Ave1 = Sum1/5
print('average state RMSE for Case1::', Ave1)

#Case2 average RMSE for different x0
arr2 = np.array([0.01750, 0.01462, 0.01363, 0.01315, 0.01281])
Sum2 = np.sum(arr2)
Ave2 = Sum2/5
print('average state RMSE for Case2::', Ave2)
#Case2 average RMSE for different x0
arrp2 = np.array([0.01745, 0.01733, 0.01679, 0.01676, 0.01664])
Sump2 = np.sum(arrp2)
Avep2 = Sump2/5
print('average parameter RMSE for Case2::', Avep2)

#Case3 average state RMSE for different x0
arr3 = np.array([0.01597, 0.01336, 0.01317,  0.01284, 0.01262])
Sum3 = np.sum(arr3)
Ave3 = Sum3/5
print('average state RMSE for Case3::', Ave3)
#Case3 average parameters RMSE for different x0
arrp3 = np.array([0.00765, 0.00769, 0.00789,  0.00777, 0.00769])
Sump3 = np.sum(arrp3)
Avep3 = Sump3/5
print('average paameter RMSE for Case3::', Avep3)

##RMSE values
stateC1=0.02827
stateC2=0.01434 
stateC3=0.01359

paramC1=0.04865
paramC2=0.01699
paramC3=0.00774

##%improvement - states
#Case3 over Case1
Improvement_percen_C3_C1 = ((stateC1 - stateC3)/stateC1)*100
print('Improvement_percen_C3_C1::', Improvement_percen_C3_C1)

#Case3 over Case2
Improvement_percen_C3_C2 = ((stateC2 - stateC3)/stateC2)*100
print('Improvement_percen_C3_C2::', Improvement_percen_C3_C2)


#Case3 over Case1 - parameters
Improvement_percen_Cp3_Cp1 = ((paramC1 - paramC3)/paramC1)*100
print('Improvement_percen_param_C3_C1::', Improvement_percen_Cp3_Cp1)

#Case3 over Case2 - parameters
Improvement_percen_Cp3_Cp2 = ((paramC2 - paramC3)/paramC2)*100
print('Improvement_percen_param_C3_C2::', Improvement_percen_Cp3_Cp2)

















